package com.nhncorp.pleiades.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;

public class LineSelector {

  public static void main(String[] args) throws IOException {

    Random random = new Random(System.nanoTime());
    File file = new File(args[0]);
    long totalLineNumber = Long.parseLong(args[1]);
    int numOfLineToGet = 100;
    if (args.length > 2) {
      numOfLineToGet = Integer.parseInt(args[2]);
    }

    FileInputStream fin = new FileInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));

    SortedSet<Long> lineSets = new TreeSet<Long>();
    long number = 0;
    for (int index = 0; index < numOfLineToGet; ++index) {
      number = random.nextLong();
      number = (number % totalLineNumber);
      lineSets.add(new Long(Math.abs(number)));
    }
    
    String lineString = null;
    long lineCount = 0;
    while ((lineString = br.readLine()) != null) {
      ++lineCount;
      if (lineSets.contains(new Long(lineCount))) {
        System.out.println(lineString);
      }
      if (lineCount >= totalLineNumber) {
        break;
      }
    }
  }
}
